package com.zonta.finance.admin.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.zonta.finance.common.dto.ApiResponse;
import com.zonta.finance.common.dto.PageInfo;
import com.zonta.finance.domain.form.*;
import com.zonta.finance.domain.vo.GetRoleDetailVo;
import com.zonta.finance.domain.vo.ListRoleVo;
import com.zonta.finance.domain.vo.MenuDataItemVo;
import com.zonta.finance.serivce.ISysRoleService;
import groovyjarjarantlr4.v4.runtime.misc.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 系统角色 前端控制器
 * </p>
 *
 * @author zonta
 * @since 2024-07-25
 */

@Api(tags = "角色管理")
@RestController
@RequestMapping(value = "/sysRole")
@RequiredArgsConstructor
@Slf4j
@Validated
public class SysRoleController {
    private final ISysRoleService sysRoleService;
    @ApiOperation(value = "新增角色")
    @PostMapping(value = "/create")
    public ApiResponse<Boolean> create(@Valid @RequestBody CreateSysRoleForm form) throws JsonProcessingException {
        return ApiResponse.success(sysRoleService.createByFrom(form));
    }

    @ApiOperation(value = "查看角色列表")
    @PostMapping(value = "/list")
    public ApiResponse<PageInfo<ListRoleVo>> ListByFrom(@Valid @RequestBody ListRoleForm form) {
        return ApiResponse.success(sysRoleService.ListByFrom(form));
    }

    @ApiOperation(value = "删除角色")
    @PostMapping(value = "/del")
    public ApiResponse<Boolean> del(@Valid @RequestBody DelSysRoleForm form) {
        return ApiResponse.success(sysRoleService.del(form.getId()));
    }

    @ApiOperation(value = "禁用或启用角色")
    @PostMapping(value = "/updateDisable")
    public ApiResponse<Boolean> updateDisable(@Valid @RequestBody UpdateRoleDisableForm form) throws JsonProcessingException {
        return ApiResponse.success(sysRoleService.updateDisable(form));
    }

    @ApiOperation(value = "修改角色")
    @PostMapping(value = "/update")
    public ApiResponse<Boolean> update(@Valid @RequestBody UpdateRoleForm form) {
        return ApiResponse.success(sysRoleService.updateByFrom(form));
    }

    @ApiOperation(value = "查询角色明细")
    @GetMapping(value = "getById")
    public ApiResponse<GetRoleDetailVo> getById(@Valid @NotNull @RequestParam Integer id) {
        return ApiResponse.success(sysRoleService.getGetRoleDetailVoById(id));
    }
    @ApiOperation(value = "查询当前登录用户角色绑定的菜单列表")
    @GetMapping(value = "/listRoleBindMenu")
    public ApiResponse<List<MenuDataItemVo>> listRoleBindMenu() {
        return ApiResponse.success(sysRoleService.listRoleBindMenu());
    }

    @ApiOperation(value = "角色绑定菜单")
    @PostMapping(value = "/roleBindMenu")
    public ApiResponse<Boolean> roleBindResource(@Valid @RequestBody RoleBindMenuForm form) {
        return ApiResponse.success(sysRoleService.roleBindMenu(form));
    }

    /**
     * 角色绑定资源
     *
     * @param form
     * @return
     */
    @PostMapping(value = "/roleBindResource")
    public ApiResponse<Boolean> roleBindResource(@Valid @RequestBody RoleBindResourceForm form) {
        return ApiResponse.success(sysRoleService.roleBindResource(form));
    }
}
